package studentinformationmanagementsystem.filter.admin;

import studentinformationmanagementsystem.entity.Admin;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author 苗庆
 * @version 1.0
 */

@WebFilter(urlPatterns = {"/admin/function/courses.do","/admin/function/student.do","/admin/function/teacher.do"

        ,"/admin/function/add-clazz.html","/admin/function/add-course.html","/admin/function/add-department.html","/admin/function/add-major.html"
        ,"/admin/function/add-student.html","/admin/function/add-subject.html","/admin/function/add-subject.jsp","/admin/function/add-teacher.html"
        ,"/admin/function/clazz.html","/admin/function/course.html","/admin/function/departments.html","/admin/function/edit-clazz.html"
        ,"/admin/function/edit-course.html","/admin/function/edit-department.html","/admin/function/edit-major.html","/admin/function/edit-student.jsp"
        ,"/admin/function/edit-subject.html","/admin/function/edit-subject.jsp","/admin/function/edit-teacher.html","/admin/function/edit-teacher.jsp"
        ,"/admin/function/student-details.html","/admin/function/student-details.jsp","/admin/function/major.html","/admin/function/profile.html"
        ,"/admin/function/index.html","/admin/function/students.jsp","/admin/function/subjects.html","/admin/function/subjects.jsp"
        ,"/admin/function/teacher-details.jsp","/admin/function/teachers.html","/admin/function/teachers.jsp","/admin/function/add-class-schedue.html"
        ,"/admin/function/edit-class-schedule.html","/admin/function/quall-class-schedule.jsp"})
public class AdminFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    //拦截未登录请求
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletResponse resp = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;

        ServletContext servletContext = request.getServletContext();
        Admin admin = (Admin) req.getSession().getAttribute("Adminlogin");
        //判断当前是否有用户进行了登陆
        if (admin != null) {
            chain.doFilter(request, response);  //已经登陆则可以访问其资源
            return;
        }
        //否则就返回到登陆页面
        resp.sendRedirect(req.getContextPath() + "/admin/log/login.html");
    }

    @Override
    public void destroy() {

    }

}
